/**
 * INFORMATIONS
 */
/**
 * @author Stefan
 *
 */
package sfx;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

public class SoundManager
{
	
	//==================================================
	// Data members
	//==================================================
	public static HashMap<String, Clip> data_clips = new HashMap<String, Clip>();
	public static boolean soundState = true;
	
	
	public static HashMap<String, Clip> getData_clips() {
		return data_clips;
	}

	public static void setData_clips(HashMap<String, Clip> data_clips) {
		SoundManager.data_clips = data_clips;
	}


	//==================================================
	// Adds a wave sound into memory
	//==================================================
	public static void addWaveSound(String key, String path)
	{
		AudioInputStream audio = null;
		Clip clip = null;
		
		try 
		{
			clip = AudioSystem.getClip();
			audio = AudioSystem.getAudioInputStream(new File(path));
			clip.open(audio);
			
		} 
		catch (LineUnavailableException e) 
		{
			e.printStackTrace();
		}
		catch (UnsupportedAudioFileException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		SoundManager.data_clips.put(key, clip);
		
	}
	
	public static void setSoundState(boolean state, String key)
	{
		SoundManager.soundState = state;
		if (SoundManager.soundState == true)
		{
			SoundManager.stop(key);
			SoundManager.data_clips.get(key).start();
		}
		else
		{
			SoundManager.data_clips.get(key).setFramePosition(0);
			SoundManager.data_clips.get(key).stop();
		}
	}
	
	
	//==================================================
	// Plays a music from the begining
	//==================================================
	public static void play(String key)
	{
		SoundManager.stop(key);
		SoundManager.data_clips.get(key).start();
	}
	
	//==================================================
	// Plays a music from the begining several times
	//==================================================
	public static void loop(String key)
	{

		SoundManager.data_clips.get(key).setLoopPoints(0, -1);
		SoundManager.data_clips.get(key).loop(1000);
	}
	
	//==================================================
	// Stops the execution of the music, puts the
	//	 frameposition at the begining
	//==================================================
	public static void stop(String key)
	{
		SoundManager.data_clips.get(key).setFramePosition(0);
		SoundManager.data_clips.get(key).stop();
	}
	
	//==================================================
	// Pause the music (use resume() to ... resume)
	//==================================================
	public static void pause(String key)
	{
		SoundManager.data_clips.get(key).stop();
	}
	
	//==================================================
	// Resumes a paused music
	//==================================================
	public static void resume(String key)
	{
		SoundManager.data_clips.get(key).start();
	}
	
	//==================================================
	// Main for testing
	//==================================================
	public static void main(String[] args) 
	{
		JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        SoundManager.addWaveSound("Intro", "Intro.wav");
        SoundManager.addWaveSound("l", "Level1.wav");

   
        SoundManager.loop("Intro");
        
        SoundManager.play("l");
	}

}
